package zmee.pur.src.formplugin;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.IFormView;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import zmee.pur.src.formplugin.filter.FilterByRule;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: xw
 * @Date: 2022/11/17/9:29
 * @Description: 开商务标节点自动添加议价
 */
public class PurListAutoAddnegBillPlugin extends AbstractBillPlugIn {

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        IFormView parentView = this.getView().getParentView();
        if (parentView != null) {
            DynamicObject dataEntity = parentView.getModel().getDataEntity();
            String name = dataEntity.getDataEntityType().getName();
            if ("src_compare".equals(name)) {
                //项目信息.寻源方式.number
                String sourceTypeNumber = dataEntity.getDynamicObject("sourcetype").getString("number");
                if ("C010406".equals(sourceTypeNumber) || "C010415".equals(sourceTypeNumber)) {
                    String billStatus = dataEntity.getString("billstatus");
                    if (billStatus == null || "".equals(billStatus) || "A".equals(billStatus)) {
                        String billNo = dataEntity.getString("billno");
                        long projectId = dataEntity.getLong("id");
                        QFilter filter = new QFilter("project.billno", "=", billNo).and("billstatus", "=", "A");
                        boolean isExists = QueryServiceHelper.exists("src_negotiatebill", filter.toArray());
                        if (isExists) {
                            //暂存
                            //查看议价单
                            this.getView().invokeOperation("viewnegbill");
                        } else {
                            QFilter filter1 = new QFilter("project.billno", "=", billNo);
                            boolean isExist = QueryServiceHelper.exists("src_negotiatebill", filter1.toArray());
                            if (!isExist) {
                                //没有议价单
                                //自动添加议价
                                this.getView().invokeOperation("autoaddnegbill");
                                this.getView().invokeOperation("viewnegbill");
                            } else {
                                int size = QueryServiceHelper.query("src_negotiatebill", "id", filter1.toArray()).size();
                                //已议价且已议标开标
                                filter1.and("isquotebidopen", "=", "1");
                                int size1 = QueryServiceHelper.query("src_negotiatebill", "id", filter1.toArray()).size();
                                if (size == size1) {
                                    QFilter filter2 = new QFilter("project.billno", "=", billNo);
                                    QFilter compareFilter = new QFilter("id", "=", projectId);
                                    DynamicObject projectRule = BusinessDataServiceHelper.loadSingle("src_project_rule", "id,extfilter",
                                            compareFilter.toArray());
                                    Set<Long> ids = new HashSet<Long>();
                                    if ("C010406".equals(sourceTypeNumber)) {
                                        String number = projectRule.getDynamicObject("extfilter").getString("number");
                                        if ("negotiate002".equals(number)) {
                                            ids = FilterByRule.getIdsByPrice(filter2, compareFilter, projectId);
                                        } else {
                                            ids = FilterByRule.getIdsByPrice(filter2, compareFilter);
                                        }
                                    } else {
                                        ids = FilterByRule.getIdsByScore(filter2, compareFilter, projectId);
                                    }
                                    if (ids != null && !ids.isEmpty()) {
                                        String number = projectRule.getDynamicObject("extfilter").getString("number");
                                        if ("negotiate004".equals(number)) {
                                            QFilter filter3 = new QFilter("entryid", "in", ids);
                                            DynamicObject[] objects = BusinessDataServiceHelper.load("src_analysetool", "material",
                                                    filter3.toArray());
                                            Map<Long, List<DynamicObject>> collect =
                                                    Arrays.stream(objects).collect(Collectors.groupingBy(o -> o.getLong("material.id")));
                                            for (Map.Entry<Long, List<DynamicObject>> item : collect.entrySet()) {
                                                List<DynamicObject> value = item.getValue();
                                                if (value.size() > 1) {
                                                    this.getView().invokeOperation("autoaddnegbill");
                                                    this.getView().invokeOperation("viewnegbill");
                                                    return;
                                                }
                                            }
                                        } else {
                                            DynamicObject pruList = BusinessDataServiceHelper.loadSingle("src_purlist_stand", "material",
                                                    new QFilter[]{new QFilter("parentid", "=", projectId)});
                                            Set<Long> mIds =
                                                    pruList.getDynamicObjectCollection("entryentity").stream().map(p -> p.getLong(
                                                            "material.id")).collect(Collectors.toSet());
                                            if (ids.size() != mIds.size()) {
                                                this.getView().invokeOperation("autoaddnegbill");
                                                this.getView().invokeOperation("viewnegbill");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}